<template>
    <div class = "personalcenter">
              <el-container >
            <el-container>
            <el-main>
                <el-col>
                <el-card shadow="hover">
                <div class = "Box">
                    <div class = "title-wrapper">
                        <p>关于我</p>
                    </div>
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="50px" class="demo-ruleForm">
                <el-form-item label="头像" >
                    <el-upload
                    :action="this.IP"
                    list-type="picture-card"
                    :data="{type:'image'}"
                    :limit="1"
                    :file-list="headImage"
                    :on-preview="handlePictureCardPreview"
                    :on-remove="handleRemove"
                    :on-success="handleAvatarSuccess">
                    <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="ruleForm.dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>
                <el-form-item label="姓名" prop = "name">
                    <el-input  v-model="ruleForm.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="性别" >
                 <el-select  v-model="ruleForm.gender" placeholder="请选择区域">
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                   >
                  </el-option>
                </el-select>
                </el-form-item>
                <el-form-item style="float:right">
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                </el-form-item>
                </el-form>
                </div>
                </el-card>
                </el-col>
            </el-main>
            </el-container>
              </el-container>
    </div>
</template>
<script>
export default {
    data(){
      var validatename = (rule, value, callback) => {
        if (value.length > 12) {
          callback(new Error('不得大于12个字'));
        } else if(value === '') {
          callback(new Error('请输入姓名'));
        } else {
          callback();
        }
      };
        return{
        dialogVisible:false,
        ruleForm: {
          name: '',
          dialogImageUrl:'',
          gender:''
        },
        rules: {
          name: [
            { validator: validatename, trigger: 'blur' }
          ],
        },
        headImage:[],
        imageUrl:'',
        options: [{
          value: 'BOY',
          label: '男生',
        }, {
          value: 'GIRL',
          label: '女生'
        }],
        }
    },
    created() {
        this.List()
    },
    methods:{
        List(){
            let form = JSON.parse(sessionStorage.getItem('user'))
            if(form){
              this.ruleForm = form
              if(this.ruleForm.gender === 'UNKNOWN'){
                this.ruleForm.gender = 'BOY'
              }
              this.headImage = [{url:form.headImage}]
              this.ruleForm.dialogImageUrl = form.imageId   
            }
        },
        open1(title,type) {
        this.$message({
          message: title,
          type: type
        });
        },
        submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
              let Add = {
                name:this.ruleForm.name,
                gender:this.ruleForm.gender,
                headImgId:this.ruleForm.dialogImageUrl,
              }
              this.$api.personalcenter.personaldetails(Add,this.ruleForm.id).then(res => {
                  if(res.code === 200){
                    sessionStorage.setItem('user', JSON.stringify(res.data))
                    this.open1('修改成功','success')
                    this.$router.push('/');
                  } else {
                    this.open1('密码错误','warning')
                  }
              })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      handleAvatarSuccess(response, file, fileList){
            this.ruleForm.dialogImageUrl = file.response.data.id
        },
        handleRemove(file, fileList) {
            this.ruleForm.dialogImageUrl = ''
        },
        handlePictureCardPreview(file) {
            this.ruleForm.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
    }
}
</script>
<style scoped>
    .personalcenter{
        width:100%;
    }
    .personalcenter .title-wrapper{
        padding:15px 0;
        margin-bottom: 20px;
        border-bottom:1px solid #e6ebf5
    }
</style>

